package com.black.cat.jvm.classfile.constantpool.impl;


import com.black.cat.jvm.classfile.ClassReader;
import com.black.cat.jvm.classfile.constantpool.BaseConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantPool;
import com.black.cat.jvm.common.CommonValue;

/**
 * @Author blackcat
 * @create 2021/8/11 20:40
 * @version: 1.0
 * @description:字段的符号引用 CONSTANT_Fieldref_info {
 * u1 tag;
 * u2 class_index;
 * u2 name_and_type_index;
 * }
 */
public class ConstantInfoFieldref extends BaseConstantInfo implements ConstantInfo {
    private ConstantPool constantPool;
    private int classIndex; // 类索引 u2
    private int nameAndTypeIndex;// 字段索引 u2

    public ConstantInfoFieldref(ConstantPool constantPool, ClassReader reader) {
        this.constantPool = constantPool;
        this.classIndex = reader.nextU2ToInt();
        this.nameAndTypeIndex = reader.nextU2ToInt();
    }

    @Override
    public String getValue() {
        value = constantPool.getUtfStringByIndex(classIndex) + ";" + constantPool.getUtfStringByIndex(nameAndTypeIndex);
        return super.getValue();
    }

    
    public String getClassName() {
        return constantPool.getUtfStringByIndex(classIndex);
    }

    public String getNameAndType() {
        return this.constantPool.getNameAndType(this.nameAndTypeIndex);
    }

    @Override
    public int getTag() {
        return CommonValue.CONSTANT_POOL_TAG_FIELD_REF;
    }
}
